﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Bll.CommonBll;
using CSP.Bll.IObjectBlls;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Common.Factories;
using CSP.Dao;
using CSP.Model.CSP;
using System.Collections.ObjectModel;

namespace CSP.Bll.ObjectBlls
{
    public class ServiceAssetBll : CSPCommonBll,IServiceAssetBll
    {
        private const string CACHEKEYALL = "ServiceAssetAll";

        private IServiceAssetDao _serviceAssetDao;

        public ServiceAssetBll()
        {
            _cacheProvider = CachedProviderFactory.GetLocalCachedProvider();
            _serviceAssetDao = CSPDaoFactory.GetInstance().GetServiceAssetDao();
        }



        public ICollection<ServiceAsset> FindByParentId(int parentId)
        {
            string cacheKey = CACHEKEYALL;
            //检测缓存是否存在
            ICollection<ServiceAsset> serviceAreas = _cacheProvider.GetData<ServiceAsset[]>(cacheKey);

            if (serviceAreas == null)
            {
                serviceAreas = FindAll();
                serviceAreas = serviceAreas.Where(s => s.Parentid == parentId).ToArray();
            }
            return serviceAreas.Where(s => s.Parentid == parentId).ToArray();

        }

        public ServiceAsset GetDataById(int id)
        {
            ServiceAsset derviceArea = GetById(id);
            if (derviceArea == null)
            {
                FindAll();
                derviceArea = GetById(id);
            }

            return derviceArea;
        }

        public ICollection<ServiceAsset> GeDatatByIds(int[] Ids)
        {
            //To Do
            throw new NotImplementedException();
        }

        public void Add(ServiceAsset obj)
        {
            string cacheKey = CACHEKEYALL;
            _serviceAssetDao.Add(obj);

            _cacheProvider.Remove(cacheKey);
        }

        public void Update(ServiceAsset obj)
        {
            string cacheKey = CACHEKEYALL;
            _serviceAssetDao.Update(obj);

            _cacheProvider.Remove(cacheKey);
        }

        public void DeleteById(int id)
        {
            var obj = _serviceAssetDao.DeleteById(id);
            string cacheKey = CACHEKEYALL;
            _cacheProvider.Remove(cacheKey);
        }

        public void RefreshCache()
        {
            throw new NotImplementedException();
        }


        public ICollection<ServiceAsset> FindAll()
        {
            //检测缓存是否存在
            ICollection<ServiceAsset> serviceAreas = _cacheProvider.GetData<ServiceAsset[]>(CACHEKEYALL);
            if (serviceAreas == null)
            {
                //从数据库读取数据
                serviceAreas = _serviceAssetDao.FindAll();
                _cacheProvider.AddData(CACHEKEYALL, serviceAreas, DateTime.Now.AddHours(2));
            }
            return serviceAreas;
        }


        /// <summary>
        /// 通过Id 获取地区
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private ServiceAsset GetById(int id)
        {
            ServiceAsset derviceArea = null;
            ICollection<ServiceAsset> serviceAreas = GetByIds(new int[] { id });
            if (serviceAreas.Count() != 0)
            {
                derviceArea = serviceAreas.FirstOrDefault();
            }

            return derviceArea;
        }

        private ICollection<ServiceAsset> GetByIds(int[] ids)
        {
            ICollection<ServiceAsset> result = new Collection<ServiceAsset>();

            //检测缓存是否存在
            ICollection<ServiceAsset> serviceAreas = _cacheProvider.GetData<ServiceAsset[]>(CACHEKEYALL);
            if (serviceAreas == null || serviceAreas.Count == 0)
            {
                serviceAreas = FindAll();
                if (serviceAreas == null || serviceAreas.Count == 0)
                {
                    return result;
                }
            }

            foreach (var item in serviceAreas)
            {
                if (ids.Contains(item.Id))
                {
                    result.Add(item);
                }
            }
            return result;
        }
    }
}
